#!/bin/bash

function error()
{
    echo "postinstall error!" "$1" 
    exit
}

#
# The shell app allows the user to select the original application that will be
# updated. It then writes the name of the original app and the path of the
# app into two separate files.
#

AppName="`cat /tmp/MacSoftInstaller/_AppName.txt`"
AppPath="`cat /tmp/MacSoftInstaller/_AppPath.txt`"
NewAppName="Age of Empires III - The WarChiefs.app"

echo Selected app to be updated: $AppPath$AppName

#
# The Installer can be a little weird about when it glues resource and data
# forks back together. On 10.3.9, it seems to do this after the scripts are
# run, which basically causes our calls to ditto to fail. So before doing
# anything, go ahead and glue any split forks back together.
#

find "/tmp/MacSoftInstaller" -type d | while read dir ; do
	/System/Library/CoreServices/FixupResourceForks "$dir"
done

#
# Copy the GameData files into the older app
#

GameDataName="`cat /tmp/MacSoftInstaller/_GameDataName.txt`"

#
# Grab a list of all the GameData files to be copied
#

#
# Do the actual copy. Copy only if the file is newer (-nt)
# Form is: [ "$newfile" -nt "$oldfile" ] && cp -- "$newfile" "$oldfile"
# Note1: Use ditto to preserve Mac creation and mod times
# Note2: ${file:22} means to drop the first 21 characters of i, which in this case would be /tmp/MacSoftInstaller
# Note3: Bash find rocks! You just have to know how to coax it to do your bidding!
#

find "/tmp/MacSoftInstaller/$GameDataName" -type f | while read file ; do
#	echo Copy From: $file
#	echo Copy To  : $AppPath$AppName/Contents/Resources/${file:22}
	[ "$file" -nt "$AppPath$AppName/Contents/Resources/${file:22}" ] && ditto -V --rsrc "$file" "$AppPath$AppName/Contents/Resources/${file:22}" 
done

#
# Update the original application with any new files we included
# Note: Because of problems with the 10.3.9 Installer messing up the mod dates
# on files with resource forks, do not use the -nt flag!
#

AppInPackage="`cat /tmp/MacSoftInstaller/_AppInPackage.txt`"

temp="/tmp/MacSoftInstaller/$AppInPackage"
len=${#temp}

find "/tmp/MacSoftInstaller/$AppInPackage" -type f | while read file ; do
#	[ "$file" -nt "$AppPath$AppName/${file:45}" ] && ditto -V --rsrc "$file" "$AppPath$AppName/${file:45}" 
	ditto -V --rsrc "$file" "$AppPath$AppName/${file:len}" 
done

#
# ditto is great, however it has some problems.  If you ditto a file that doesn't have
# a parent directory, ditto will create that directory for you, but will leave off
# some of the write permissions. So after we're done copying the new GameData
# files, walk through the directory structure and make sure all the dirs have
# the same permissions.
#

find "$AppPath$AppName/Contents/Resources/$GameDataName" -type d | while read dir ; do
	chmod 775 "$dir"
done

#
# Touch the old app so the Finder will show the updated version number
#

touch "$AppPath$AppName"

#
# Copy the new application from /tmp/MacSoftInstaller/ into the game folder
#

ditto -V --rsrc "/tmp/MacSoftInstaller/$NewAppName" "$AppPath$NewAppName"

#
# Create an alias to the original GameData folder
#

ln -s -f -h -v "$AppPath$AppName/Contents/Resources/$GameDataName" "$AppPath$NewAppName/Contents/Resources/$GameDataName"

#
# Touch the new app so the Finder will display its icon
#

touch "$AppPath$NewAppName"

#
# Clean up the /tmp folder
#

rm -r "/tmp/MacSoftInstaller"/*

exit 0
